home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / V2-4-5 / GPPLIBSR00 / cc / stream < prev    next >
Text File  |  1993-12-07  |  3KB  |  126 lines

  1.  
  2. extern "C"
  3.   {
  4.   #include <stdarg.h>
  5.   }
  6.  
  7. #include "ioprivate.h"
  8. #include "stream.h"
  9. #include "strstream.h"
  10.  
  11. static char Buffer[_G_BUFSIZ];
  12. #define EndBuffer (Buffer+_G_BUFSIZ)
  13. static char* next_chunk = Buffer; // Start of available part of Buffer.
  14.  
  15. char* form(const char* format, ...)
  16. {
  17.     int space_left = EndBuffer - next_chunk;
  18.     // If less that 25% of the space is available start over.
  19.     if (space_left < (_G_BUFSIZ>>2))
  20.     next_chunk = Buffer;
  21.     char* buf = next_chunk;
  22.  
  23.     strstreambuf stream(buf, EndBuffer-buf-1, buf);
  24.     va_list ap;
  25.     va_start(ap, format);
  26.     int count = stream.vform(format, ap);
  27.     va_end(ap);
  28.     stream.sputc(0);
  29.     next_chunk = buf + stream.pcount();
  30.     return buf;
  31. }
  32.  
  33. #define u_long unsigned long
  34.  
  35. static char* itoa(unsigned long i, int size, int neg, int base)
  36. {
  37.     // Conservative estimate: If base==2, might need 8 characters
  38.     // for each input byte, but normally 3 is plenty.
  39.     int needed = size ? size
  40.     : (base >= 8 ? 3 : 8) * sizeof(unsigned long) + 2;
  41.     int space_left = EndBuffer - next_chunk;
  42.     if (space_left <= needed)
  43.     next_chunk = Buffer; // start over.
  44.  
  45.     char* buf = next_chunk;
  46.  
  47.     register char* ptr = buf+needed+1;
  48.     next_chunk = ptr;
  49.  
  50.     if (needed < (2+neg) || ptr > EndBuffer)
  51.     return NULL;
  52.     *--ptr = 0;
  53.     
  54.     if (i == 0)
  55.     *--ptr = '0';
  56.     while (i != 0 && ptr > buf) {
  57.     int ch = i % base;
  58.     i = i / base;
  59.     if (ch >= 10)
  60.         ch += 'a' - 10;
  61.     else
  62.         ch += '0';
  63.     *--ptr = ch;
  64.     }
  65.     if (neg)
  66.     *--ptr = '-';
  67.     if (size == 0)
  68.     return ptr;
  69.     while (ptr > buf)
  70.     *--ptr = ' ';
  71.     return buf;
  72. }
  73.  
  74. char* dec(long i, int len /* = 0 */)
  75. {
  76.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  77.     else return itoa((unsigned long)(-i), len, 1, 10);
  78. }
  79. char* dec(int i, int len /* = 0 */)
  80. {
  81.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  82.     else return itoa((unsigned long)(-i), len, 1, 10);
  83. }
  84. char* dec(unsigned long i, int len /* = 0 */)
  85. {
  86.     return itoa(i, len, 0, 10);
  87. }
  88. char* dec(unsigned int i, int len /* = 0 */)
  89. {
  90.     return itoa(i, len, 0, 10);
  91. }
  92.  
  93. char* hex(long i, int len /* = 0 */)
  94. {
  95.     return itoa((unsigned long)i, len, 0, 16);
  96. }
  97. char* hex(int i, int len /* = 0 */)
  98. {
  99.     return itoa((unsigned long)i, len, 0, 16);
  100. }
  101. char* hex(unsigned long i, int len /* = 0 */)
  102. {
  103.     return itoa(i, len, 0, 16);
  104. }
  105. char* hex(unsigned int i, int len /* = 0 */)
  106. {
  107.     return itoa(i, len, 0, 16);
  108. }
  109.  
  110. char* oct(long i, int len /* = 0 */)
  111. {
  112.     return itoa((unsigned long)i, len, 0, 8);
  113. }
  114. char* oct(int i, int len /* = 0 */)
  115. {
  116.     return itoa((unsigned long)i, len, 0, 8);
  117. }
  118. char* oct(unsigned long i, int len /* = 0 */)
  119. {
  120.     return itoa(i, len, 0, 8);
  121. }
  122. char* oct(unsigned int i, int len /* = 0 */)
  123. {
  124.     return itoa(i, len, 0, 8);
  125. }
  126.